angular.module('bookshelfsearchcontrollers', [])
//更多视频的Controller
    .controller('BookshelfSearchCtrl',function($scope,$state,$rootScope,$ionicHistory,$timeout,$ionicSlideBoxDelegate,$ionicScrollDelegate,Toast,Loading,ENV,Video,History,Magazine,Storage,Member){
        var memberId ='';
        //测试数据
        $scope.videoList1 = [{
            "icon":"http://localhost:63342/wineweb/img/play.png",
            "title":'品酒人生',
            "videoList": [
                {
                    "video_title": "美酒学堂 | 葡萄酒的橡木塞与拧盖之争",
                    "vcStatus": false,
                    "video_background": "1972d0ba-c24c-4486-8125-9748915d3504.jpg",
                    "video_id": 37,
                    "video_time_length": "02:07"
                },
                {
                    "video_title": "美酒学堂 | 葡萄酒的软木塞 ",
                    "vcStatus": false,
                    "video_background": "996acf48-193a-4af7-a7b3-757f18fb4ad2.jpg",
                    "video_id": 35,
                    "video_time_length": "01:47"
                },
                {
                    "video_title": "美酒学堂 | 木塞断了咋办",
                    "vcStatus": false,
                    "video_background": "7aa23beb-6306-441a-83fb-67a93a1d4989.jpg",
                    "video_id": 34,
                    "video_time_length": "01:59"
                }
            ]
        },{
            "icon":"http://localhost:63342/wineweb/img/play.png",
            "title":'更多视频类别',
            "videoList": [
                {
                    "video_title": "美酒学堂 | 隔空取木塞",
                    "vcStatus": false,
                    "video_background": "e7e9702c-942e-486f-8415-dfacfc7cabde.jpg",
                    "video_id": 33,
                    "video_time_length": "01:50"
                },
                {
                    "video_title": "美酒学堂 | 木塞发霉酒能喝吗？",
                    "vcStatus": false,
                    "video_background": "94f816d3-0742-4d56-b69d-b59545f31f79.jpg",
                    "video_id": 32,
                    "video_time_length": "01:40"
                },
                {
                    "video_title": "美酒学堂 | 木塞凸起的葡萄酒还能喝吗？",
                    "vcStatus": false,
                    "video_background": "57668f0c-242b-4d9b-b730-bd48e55d74b1.jpg",
                    "video_id": 31,
                    "video_time_length": " 01:52"
                }
            ]
        }];
        $scope.goback = function () {
            window.history.back();
        }
        $scope.result=0;
        $scope.videoList = [];
        $scope.magazineList = [];
        $scope.bookList = [];
        $scope.picPath = ENV.picPath;
        $scope.slideChanged = function(index){
            if(index==0){
                showVideoFun();
            }else if(index==1){
                showMagazineFun();
            }if(index==2){
                showBookFun();
            }
        }
        Loading.showLoading();
        Video.getVideoList(memberId);//加载视频列表
        $scope.showWhich = [true,false,false];
        //点击搜索，搜索相应的书
        $scope.finishSelectWine=function(){
            
        }
        //进入页面的时候调用
        $scope.$on('$ionicView.beforeEnter',function(){
            $rootScope.showWineGlasses = false;
            memberId =  Storage.get('member').member_id;
            if($scope.showWhich[0]&&$scope.videoList!=undefined&&$scope.videoList.length>0){
                searchList = [];
                for(i=0;i<$scope.videoList.length;i++) {
                    search = {
                        mc_obj_id: $scope.videoList[i].video_id,
                        member_id: memberId,
                        mc_obj_type:5
                    }
                    searchList.push(search);
                }
                if(searchList.length>0){
                    Video.getCollectInfo('video',searchList);
                }
            }else if($scope.showWhich[1]&&$scope.magazineList!=undefined&&$scope.magazineList.length>0){
                searchList = [];
                for(i=0;i<$scope.magazineList.length;i++) {
                    search = {
                        mc_obj_id: $scope.magazineList[i].book_id,
                        member_id: memberId,
                        mc_obj_type:4
                    }
                    searchList.push(search);
                }
                if(searchList.length>0){
                    Video.getCollectInfo('magazine',searchList);
                }
            } else if($scope.showWhich[2]&&$scope.bookList!=undefined&&$scope.bookList.length>0){
                searchList = [];
                for(i=0;i<$scope.bookList.length;i++) {
                    search = {
                        mc_obj_id: $scope.bookList[i].book_id,
                        member_id: memberId,
                        mc_obj_type:4
                    }
                    searchList.push(search);
                }
                if(searchList.length>0){
                    Video.getCollectInfo('book',searchList);
                }
            }
        });
        $scope.$on('BookShelf.getCollectInfoUpdated',function(){
            var result = Video.getCollectInfoJson();
            var list = result.collectInfo;
            if(result.type=='video'){//视频
                for(i=0;i<$scope.videoList.length;i++){
                    for(j=0;j<list.length;j++){
                        if($scope.videoList[i].video_id==list[j].id){
                            $scope.videoList[i].vcStatus = list[j].state;
                        }
                    }
                }
            }else if(result.type=='magazine'){//杂志
                for(i=0;i<$scope.magazineList.length;i++){
                    for(j=0;j<list.length;j++){
                        if($scope.magazineList[i].book_id==list[j].id){
                            $scope.magazineList[i].mcStatus = list[j].state;
                        }
                    }
                }
            }else if(result.type=='book'){//丛书
                for(i=0;i<$scope.bookList.length;i++){
                    for(j=0;j<list.length;j++){
                        if($scope.bookList[i].book_id==list[j].id){
                            $scope.bookList[i].mcStatus = list[j].state;
                        }
                    }
                }
            }
        });
        //下拉刷新
        $scope.doRefreshVideo = function(){Video.getVideoList(memberId);}
        $scope.doRefreshMagazine = function(){Magazine.getMagazineList(memberId);}
        $scope.doRefreshBook = function(){Magazine.getBookList(memberId);}
        $scope.loadMoreVideo = function(){
            Video.getMoreVideoList(memberId);
        }
        $scope.loadMoreMagazine = function(){
            Magazine.getMoreMagazineList(memberId);
        }
        $scope.loadMoreBook = function(){
            Magazine.getBookList(memberId);
        }
        //视频是否有下一页
        $scope.videoHasNextPage = function(){
            return Video.videoHasNextPage();

        }
        //杂志是否有下一页
        $scope.magazineHasNextPage = function(){
            return Magazine.magazineHasNextPage();
        }
        //丛书是否有下一页
        $scope.bookHasNextPage = function(){
            return Magazine.bookHasNextPage();
        }

        $scope.$on('Video.MoreVideoListUpdated',function(){
            $scope.videoList = Video.getVideoListJson();
            $scope.$broadcast('scroll.infiniteScrollComplete');
        });
        $scope.$on('Magazine.MoreMagazineListUpdated',function(){
            $scope.magazineList = Magazine.getMagazineListJson();
            $scope.$broadcast('scroll.infiniteScrollComplete');
        });
        $scope.$on('Book.MoreBookListUpdated',function(){
            $scope.bookList = Magazine.getBookListJson();
            $scope.$broadcast('scroll.infiniteScrollComplete');
        });

        /*******显示视频*******/
        $scope.showVideo = function(){
            $ionicSlideBoxDelegate.slide(0,300);
            showVideoFun();
        };
        showVideoFun = function(){
            $ionicScrollDelegate.scrollTop();
            $scope.showWhich = [true,false,false];
            if($scope.videoList!=undefined&&$scope.videoList.length>0){
                searchList = [];
                for(i=0;i<$scope.videoList.length;i++) {
                    search = {
                        mc_obj_id: $scope.videoList[i].video_id,
                        member_id: memberId,
                        mc_obj_type:5
                    }
                    searchList.push(search);
                }
                if(searchList.length>0){
                    Video.getCollectInfo('video',searchList);
                }
            }
        }
        //获取网络数据成功的后调用该方法
        $scope.$on('Video.videoListUpdated', function(){
            $scope.$broadcast('scroll.refreshComplete');
            $scope.videoList = Video.getVideoListJson();
            Loading.hideLoading();
        });
        /*******显示视频结束*******/

        /*******显示杂志*********/
        var firstInMagazine = true;
        $scope.showMagazine = function(){
            $ionicSlideBoxDelegate.slide(1,300);
            showMagazineFun();
        };
        showMagazineFun = function(){
            $ionicScrollDelegate.scrollTop();
            $scope.showWhich = [false,true,false];
            if(firstInMagazine){
                firstInMagazine = false;
                Loading.showLoading();
                Magazine.getMagazineList(memberId);//加载杂志列表
            }else{
                if($scope.magazineList!=undefined&&$scope.magazineList.length>0){
                    searchList = [];
                    for(i=0;i<$scope.magazineList.length;i++) {
                        search = {
                            mc_obj_id: $scope.magazineList[i].book_id,
                            member_id: memberId,
                            mc_obj_type:4
                        }
                        searchList.push(search);
                    }
                    if(searchList.length>0){
                        Video.getCollectInfo('magazine',searchList);
                    }
                }
            }
        }
        //获取网络数据成功的后调用该方法--->获取到杂志或者丛书的列表
        $scope.$on('Magazine.magazineListUpdated', function(){
            $scope.$broadcast('scroll.refreshComplete');
            $scope.magazineList = Magazine.getMagazineListJson();
            Loading.hideLoading();
        });
        /*******显示杂志结束*********/

        /**********显示丛书********/
        var firstInBook = true;
        $scope.showBook = function(){
            $ionicSlideBoxDelegate.slide(2,300);
            showBookFun();
        };
        showBookFun = function(){
            $ionicScrollDelegate.scrollTop();
            $scope.showWhich = [false,false,true];
            if(firstInBook){
                firstInBook = false;
                Loading.showLoading();
                Magazine.getBookList(memberId);//加载丛书列表
            }else{
                if($scope.bookList!=undefined&&$scope.bookList.length>0){
                    searchList = [];
                    for(i=0;i<$scope.bookList.length;i++) {
                        search = {
                            mc_obj_id: $scope.bookList[i].book_id,
                            member_id: memberId,
                            mc_obj_type:4
                        }
                        searchList.push(search);
                    }
                    if(searchList.length>0){
                        Video.getCollectInfo('book',searchList);
                    }
                }
            }
        }
        $scope.$on('Book.bookListUpdated', function(){
            $scope.$broadcast('scroll.refreshComplete');
            $scope.bookList = Magazine.getBookListJson();
            Loading.hideLoading();
        });
        /**********显示丛书结束********/
        //收藏视频
        var collectIndex = '';
        var collectVideoing = false;
        $scope.collectVideo = function(videoId,index){
            if(!collectVideoing){
                collectVideoing = true;
                Loading.showLoading();
                collectIndex = index;
                Video.collectVideo(memberId,videoId);
            }
        };
        //获取网络数据成功的后调用该方法
        $scope.$on('Video.collectVideoUpdated',function(){
            collectVideoing = false;
            Loading.hideLoading();
            var result = Video.getCollectJson();
            $scope.videoList[collectIndex].vcStatus = result.vcStatus;
            Toast.showMessage(result.vcMsg);
        });
        //收藏杂志or收藏丛书
        var collectBooking = false;
        $scope.collectBook = function(bookId,bookType,index){
            if(!collectBooking){
                collectBooking = true;
                Loading.showLoading();
                collectIndex = index;
                Magazine.collectBook(memberId,bookId,bookType);
            }
        };
        //获取网络数据成功的后调用该方法
        $scope.$on('Magazine.CollectMagazineUpdated',function(){
            collectBooking = false;
            Loading.hideLoading();
            var result = Magazine.getCollectMagazineJson();
            if($scope.showWhich[1]){
                $scope.magazineList[collectIndex].mcStatus = result.mcStatus;
            }else if($scope.showWhich[2]){
                $scope.bookList[collectIndex].mcStatus = result.mcStatus;
            }
            Toast.showMessage(result.mcMsg);
        });
    }).filter(
    'to_trusted', ['$sce', function ($sce) {
        return function (text) {
            return $sce.trustAsHtml(text);
        }
    }]);



